﻿// --------------------------------
// <copyright file="EntityDataModelProductRepository.cs" company="private">
//     Microsoft Public License (Ms-PL)
// </copyright>
// <author>Glenn</author>
// ---------------------------------

using System.Configuration;
using System.Linq;

namespace OpenTicket.Repositories
{
    /// <summary>
    /// Repository for product connected to ADO.NET Entity Framwork
    /// </summary>
    public class EntityDataModelProductRepository : EntityDataModelRepositoryBase, IProductRepository 
    {
        /// <summary>
        /// The culture to use for productname etc.
        /// </summary>
        ////TODO:Do something about this, Structuremap?
        private string culture = ConfigurationManager.AppSettings["Culture"];

        /// <summary>
        /// Gets all Products from persistance
        /// </summary>
        /// <returns>All Products</returns>
        public IQueryable<OpenTicket.Model.Product> GetAll()
        {
            return from p in Edm.Product
                   join pl in Edm.ProductLanguage on p.ProductCode equals pl.ProductCode
                   where pl.Culture == this.culture
                     select new OpenTicket.Model.Product
                     {
                         ProductCost = p.ProductCost,
                         ProductCode = p.ProductCode,
                         ProductName = pl.ProductName
                     };
        }

        /// <summary>
        /// Saves the specified product.
        /// </summary>
        /// <param name="product">The product.</param>
        /// <returns>The saved product</returns>
        public Model.Product Save(Model.Product product)
        {
            var productEntity =
                     Edm.Product.Include("ProductLanguage").Where(x => x.ProductCode == product.ProductCode).FirstOrDefault();
            bool isNew = false;
            if (productEntity == null)
            {
                productEntity = new Entitymodel.Product();
                productEntity.ProductCode = product.ProductCode;
                isNew = true;
            }

            this.PopulateProductEntity(product, productEntity);

            if (isNew)
            {
                Edm.AddToProduct(productEntity);
            }

            Edm.SaveChanges();

            return product;
        }

        /// <summary>
        /// Deletes the product.
        /// </summary>
        /// <param name="product">The product.</param>
        public void DeleteProduct(OpenTicket.Model.Product product)
        {
            var dbproduct = Edm.Product.Where(x => x.ProductCode == product.ProductCode).FirstOrDefault();

            ////THINKOF:Maybe compare here to see if product has changed since loaded last time

            Edm.DeleteObject(dbproduct);
            Edm.SaveChanges();            
        }

        /// <summary>
        /// Populates the product entity.
        /// </summary>
        /// <param name="product">The product.</param>
        /// <param name="productEntity">The product entity.</param>
        private void PopulateProductEntity(Model.Product product, Entitymodel.Product productEntity)
        {
            productEntity.ProductCost = product.ProductCost;

            var cultureEntity = productEntity.ProductLanguage.Where(x => x.Culture == this.culture && x.ProductCode == productEntity.ProductCode).FirstOrDefault();

            if (cultureEntity != null)
            {
                cultureEntity.ProductName = product.ProductName;
            }
            else
            {
                Edm.AddToProductLanguage(new Entitymodel.ProductLanguage { Culture = this.culture, ProductName = product.ProductName, Product = productEntity });
            }
        }
    }
}
